package com.bianguo.android.apushdemo.view.activity;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.widget.ImageView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.adapter.MapAdapter;
import com.bianguo.android.apushdemo.utils.MyLog;

import java.util.ArrayList;

import butterknife.BindView;

public class DesignActivity extends BaseActivity implements AppBarLayout.OnOffsetChangedListener {

    @BindView(R.id.design_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.design_appbar_layout)
    AppBarLayout mAppBarLayout;
    @BindView(R.id.design_collapsing)
    CollapsingToolbarLayout mCollapsingLayout;
    @BindView(R.id.design_icon)
    ImageView iconIv;
    @BindView(R.id.design_recycler)
    RecyclerView mRecycler;

    private ArrayList<String> mList;
    private MapAdapter adapter;

    @Override
    public int layoutId() {
        return R.layout.activity_design;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        setSupportActionBar(mToolbar);
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mList.add("item " + i);
        }
        adapter = new MapAdapter(mContext, mList);
        mRecycler.setAdapter(adapter);
        mCollapsingLayout.setCollapsedTitleGravity(Gravity.CENTER_HORIZONTAL);
        mCollapsingLayout.setCollapsedTitleTextColor(Color.TRANSPARENT);
        mAppBarLayout.addOnOffsetChangedListener(this);
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        MyLog.mLog("verticalOffset = " + verticalOffset);
        float alpha = Math.abs(verticalOffset) / 1000.0f * 2;
        iconIv.setAlpha(alpha);
        if (0 == verticalOffset) {
            //展开状态
        } else if (appBarLayout.getTotalScrollRange() == Math.abs(verticalOffset)) {
            //折叠状态
            iconIv.setAlpha(1.0f);
        }
    }
}
